package com.game.connection.message;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Message {

	private String txt;

	private int id;

	public List<BulletMessage> getBullets() {
		return bullets;
	}

	public void setBullets(List<BulletMessage> bullets) {
		this.bullets = bullets;
	}

	private String name;

	private double x;

	private double y;

	private double faceAngle;

	private String message;

	private double velX;

	private double velY;

	private List<BulletMessage> bullets;

	public String getTxt() {
		return txt;
	}

	public void setTxt(String txt) {
		this.txt = txt;
	}

	public double getVelX() {
		return velX;
	}

	public void setVelX(double velX) {
		this.velX = velX;
	}

	public double getVelY() {
		return velY;
	}

	public void setVelY(double velY) {
		this.velY = velY;
	}

	public Message() {
		bullets = new ArrayList<BulletMessage>();
	}

	public Message(int id, String txt) {
		this();
		this.txt = txt;
		this.id = id;
		load();

	}

	protected String removeIdentification(String valor) {
		return valor.replaceAll("\\w+:", "");
	}

	public void load() {
		String bulletsMsg = "";
		String message = "";
		if (this.txt.indexOf("bullets") > -1) {
			bulletsMsg = this.txt.substring(this.txt.indexOf("bullets"))
					.replace("bullets", "").replace("{", "");
			message = this.txt.substring(0, this.txt.indexOf("bullets"));
		} else {
			message = this.txt;
		}

		StringTokenizer token = new StringTokenizer(message, "##&#");
		String value;
		while (token.hasMoreElements()) {

			value = (String) token.nextElement();
			if (value.indexOf("id:") > -1) {
				value = removeIdentification(value);
				setId(parseStringToInteger(value));
			}

			if (value.indexOf("name:") > -1) {
				value = removeIdentification(value.replace("null", ""));
				setName(value);
			}

			if (value.indexOf("velX:") > -1) {
				value = removeIdentification(value);
				setVelX(parseStringToDouble(value));
			}

			if (value.indexOf("velY:") > -1) {
				value = removeIdentification(value);
				setVelY(parseStringToDouble(value));
			}

			if (value.indexOf("faceAngle:") > -1) {
				value = removeIdentification(value);
				setFaceAngle(parseStringToDouble(value));
			}

			if (value.indexOf("x:") > -1) {
				value = removeIdentification(value);
				setX(parseStringToDouble(value));
			}

			if (value.indexOf("y:") > -1) {
				value = removeIdentification(value);
				setY(parseStringToDouble(value));
			}
			if (value.indexOf("msg:") > -1) {
				value = removeIdentification(value.replace("null", ""));
				setMessage(value);
			}

		}

		if (bulletsMsg.length() > 6) {
			token = new StringTokenizer(bulletsMsg, ",");
			value = "";

			while (token.hasMoreElements()) {
				value = (String) token.nextElement();
				BulletMessage bull = new BulletMessage();
				bull.setTxt(value);
				bull.load();
				bullets.add(bull);
			}
		}

	}

	public String write() {

		String bulletTxt = ";bullets{";

		for (int i = 0; i < bullets.size(); i++) {
			bulletTxt += bullets.get(i).write();

			if (i < bullets.size() - 1)
				bulletTxt += ",";

		}
		bulletTxt += "}##&#}";
		return "mensagem{player{##&#id:" + getId() + "##&#;##&#name:"
				+ getName() + "##&#;##&#velX:" + getVelX() + "##&#;##&#velY:"
				+ getVelY() + "##&#;##&#faceAngle:" + getFaceAngle()
				+ "##&#;##&#x:" + getX() + "##&#;##&#y:" + getY()
				+ "##&#;##&#msg:" + getMessage() + "##&#" + bulletTxt;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getX() {
		return x;
	}

	public double getFaceAngle() {
		return faceAngle;
	}

	public Double parseStringToDouble(String value) {
		Double val;
		try {
			val = new Double(value.trim());
		} catch (Exception e) {
			val = new Double(0);
		}
		return val;
	}
	
	public boolean parseStringToBoolean(String value) {
		Boolean val;
		try {
			val = new Boolean(value.trim());
		} catch (Exception e) {
			val = new Boolean(false);
		}
		return val;
	}

	public Integer parseStringToInteger(String value) {
		Integer val;
		try {
			val = new Integer(value.trim());
		} catch (Exception e) {
			val = new Integer(0);
		}
		return val;
	}

	public void setFaceAngle(double faceAngle) {
		this.faceAngle = faceAngle;
	}

	public void setX(double x) {
		this.x = x;
	}

	public double getY() {
		return y;
	}

	public void setY(double y) {
		this.y = y;

	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public static void main(String[] args) {
		Message msg = new Message(
				2,
				"mensagem{player{##&#id:0##&#;##&#name:##&#;##&#velX:3.6739403974420595E-17##&#;##&#velY:-0.6##&#;##&#faceAngle:0.0##&#;##&#x:320.0##&#;##&#y:226.90000000000006##&#;##&#msg:##&#;bullets{}##&#}");
		// "mensagem{player{##&#id:0##&#;##&#name:null##&#;##&#velX:3.6739403974420595E-17##&#;##&#velY:-0.6##&#;##&#faceAngle:0.0##&#;##&#x:320.0##&#;##&#y:226.90000000000006##&#;##&#msg:null##&#}}");
		System.out.println(msg.write());
	}

	@Override
	public String toString() {
		return "Message [id=" + id + ", name=" + name + ", x=" + x + ", y=" + y
				+ ", faceAngle=" + faceAngle + ", message=" + message
				+ ", velX=" + velX + ", velY=" + velY // + ", +txt=" + txt
				+ " ,]";
	}

}
